package cn.com.sise.controller;

import cn.com.sise.pojo.*;
import cn.com.sise.service.*;
import cn.com.sise.utils.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.faces.annotation.RequestMap;
import javax.json.Json;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    private UserService userService;
    private FeedbackService feedbackService;
    private AssistanceService assistanceService;
    private ResourcesService resourcesService;
    private ExerciseService exerciseService;
    private DictionaryService dictionaryService;
    private FavouriteHistoryService favouriteHistoryService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setFeedbackService(FeedbackService feedbackService) {
        this.feedbackService = feedbackService;
    }

    @Autowired
    public void setAssistanceService(AssistanceService assistanceService) {
        this.assistanceService = assistanceService;
    }

    @Autowired
    public void setResourcesService(ResourcesService resourcesService) {
        this.resourcesService = resourcesService;
    }

    @Autowired
    public void setExerciseService(ExerciseService exerciseService) {
        this.exerciseService = exerciseService;
    }

    @Autowired
    public void setDictionaryService(DictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    @Autowired
    public void setFavouriteHistoryService(FavouriteHistoryService favouriteHistoryService) {
        this.favouriteHistoryService = favouriteHistoryService;
    }

    /**
     * 用户登录
     *
     * @param user 前端获取的用户名和密码
     * @return 返回Json
     */
    @RequestMapping("/doLogin")
    public @ResponseBody
    JsonResult<User> login(@RequestBody User user) {
        System.err.println("====================用户登录(用户名:" +
                user.getName() + ",密码:" + user.getPassword() + ")====================");
        User user1 = userService.login(user.getName(), user.getPassword());
        if (user1 != null) {
            System.err.println("====================用户登录成功====================");
            return JsonResult.success(user1);
        } else {
            System.err.println("====================用户登录失败====================");
            return JsonResult.error("用户名或密码错误！");
        }
    }

    /**
     * 用户注册
     *
     * @param user 注册传入的注册信息
     * @return 返回Json
     */
    @RequestMapping("/doRegister")
    public @ResponseBody
    JsonResult<T> Register(@RequestBody User user) {
        System.err.println("====================用户注册====================");
        User u = userService.getUserByName(user.getName());
        if (u == null) {
            int flag = userService.insertUser(user);
            if (flag > 0) {
                System.err.println("====================用户[" + user.getName()
                        + "]注册成功====================");
                return JsonResult.success("注册成功!");
            } else {
                System.err.println("====================用户[" + user.getName()
                        + "]注册失败====================");
                return JsonResult.error("注册失败!");
            }
        } else {
            System.err.println("====================用户名[" + user.getName()
                    + "]已被注册====================");
            return JsonResult.error("该用户已被注册!");
        }
    }

    /**
     * 用户修改资料
     *
     * @param user 传入的用户信息
     * @return 返回Json
     */
    @RequestMapping("/doModifyUserInfo")
    public @ResponseBody
    JsonResult<User> doModifyUserInfo(@RequestBody User user) {
        if (userService.updateUser(user) > 0) {
            User result = userService.selectUserById(new User(user.getUserId()));
            System.err.println("用户[" + user.getName() + "]修改成功,修改后信息为:" + result);
            return JsonResult.success(result);
        } else {
            return JsonResult.error("修改失败!");
        }
    }

    /**
     * 用户提出反馈
     *
     * @param feedback 反馈信息
     * @return 返回Json
     */
    @RequestMapping("/doFeedback")
    public @ResponseBody
    JsonResult<T> doFeedback(@RequestBody Feedback feedback) {
        if (feedbackService.insertFeedback(feedback) > 0) {
            System.err.println("用户" + feedback.getUserId() + "反馈了:" + feedback.getDetails());
            return JsonResult.success();
        } else {
            System.err.println("用户" + feedback.getUserId() + "反馈失败！");
            return JsonResult.error();
        }
    }

    /**
     * 根据userId查询该用户所有反馈
     *
     * @param feedback 用户ID
     * @return 返回Json
     */
    @RequestMapping("/doFeedbackInfo")
    public @ResponseBody
    JsonResult<List<Feedback>> doFeedbackInfo(@RequestBody Feedback feedback) {
        List<Feedback> feedbackList = feedbackService.getFeedbackById(feedback.getUserId());
        System.err.println("用户[" + feedback.getUserId() + "]反馈为:" + feedbackList);
        return JsonResult.successForList(feedbackList);
    }

    /**
     * 获取所有的帮助
     *
     * @return 返回Json
     */
    @RequestMapping("/doAssistance")
    public @ResponseBody
    JsonResult<List<Assistance>> doAssistance() {
        System.err.println("客户端正在获取所有的帮助");
        return JsonResult.successForList(assistanceService.getAllAssistance());
    }

    /**
     * 获取所有的视听资源
     *
     * @return 返回Json
     */
    @RequestMapping("/doResources")
    public @ResponseBody
    JsonResult<List<Resources>> doResources() {
        System.err.println("客户端正在获取所有的视听资源");
        return JsonResult.successForList(resourcesService.getAllResource());
    }

    /**
     * 获取所有的试卷信息
     *
     * @return 返回Json
     */
    @RequestMapping("/doExercise")
    public @ResponseBody
    JsonResult<List<Exercise>> doExercise() {
        System.err.println("客户端正在获取所有的试卷信息");
        return JsonResult.successForList(exerciseService.getAllExercises());
    }

    /**
     * 获取指定ID的所有题目信息
     *
     * @param exercise 获取试卷ID
     * @return 返回Json
     */
    @RequestMapping("/doShowExercise")
    public @ResponseBody
    JsonResult<List<Exercise>> doShowExercise(@RequestBody Exercise exercise) {
        System.err.println("客户端正在获取ID为[" + exercise.getExerciseId() + "]的试卷的所有题目");
        return JsonResult.successForList(exerciseService.getAllQuestionById(exercise.getExerciseId()));
    }

    /**
     * 获取指定用户的收藏单词
     *
     * @param user 用户ID
     * @return 返回Json
     */
    @RequestMapping("/doShowFavouriteWord")
    public @ResponseBody
    JsonResult<User> doShowFavouriteWord(@RequestBody User user) {
        if (userService.getFavoritesDictionaryByUserId(user.getUserId()).isEmpty()) {
            System.err.println("用户:[" + user.getUserId() + "]没有收藏单词");
            return JsonResult.error("该用户没有收藏单词");
        } else {
            System.err.println("用户:[" + user.getUserId() + "]正在获取收藏单词");
            return JsonResult.success(userService.getFavoritesDictionaryByUserId(user.getUserId()).get(0));
        }
    }

    /**
     * 获取指定用户的收藏视听
     *
     * @param user 用户ID
     * @return 返回Json
     */
    @RequestMapping("/doShowFavouriteResources")
    public @ResponseBody
    JsonResult<User> doShowFavouriteResources(@RequestBody User user) {
        if (userService.getFavoritesResourcesByUserId(user.getUserId()).isEmpty()) {
            System.err.println("用户:[" + user.getUserId() + "]没有收藏视听");
            return JsonResult.error("该用户没有收藏视听");
        } else {
            System.err.println("用户:[" + user.getUserId() + "]正在获取收藏的视听");
            return JsonResult.success(userService.getFavoritesResourcesByUserId(user.getUserId()).get(0));
        }
    }

    /**
     * 获取指定用户的历史单词
     *
     * @param user 用户的ID
     * @return 返回Json
     */
    @RequestMapping("/doShowHistoryWord")
    public @ResponseBody
    JsonResult<User> doShowHistoryWord(@RequestBody User user) {
        if (userService.getHistoryDictionaryByUserId(user.getUserId()).isEmpty()) {
            System.err.println("用户:[" + user.getUserId() + "]没有历史单词");
            return JsonResult.error("该用户没有历史单词");
        } else {
            System.err.println("用户:[" + user.getUserId() + "]正在获取历史单词");
            return JsonResult.success(userService.getHistoryDictionaryByUserId(user.getUserId()).get(0));
        }
    }

    /**
     * 获取指定用户的历史视听
     *
     * @param user 用户ID
     * @return 返回Json
     */
    @RequestMapping("/doShowHistoryResources")
    public @ResponseBody
    JsonResult<User> doShowHistoryResources(@RequestBody User user) {
        if (userService.getHistoryResourcesByUserId(user.getUserId()).isEmpty()) {
            System.err.println("用户:[" + user.getUserId() + "]没有历史视听");
            return JsonResult.error("该用户没有历史视听");
        } else {
            System.err.println("用户:[" + user.getUserId() + "]正在获取历史的视听");
            return JsonResult.success(userService.getHistoryResourcesByUserId(user.getUserId()).get(0));
        }
    }

    /**
     * 查询单词
     *
     * @param dictionary 传入的单词或翻译
     * @return 返回Json
     */
    @RequestMapping("/doTranslation")
    public @ResponseBody
    JsonResult<Dictionary> doTranslation(@RequestBody Dictionary dictionary) {
        if (dictionary.getWord() == null) {
            System.err.println("传入的单词为空，开始进行翻译模糊查询...");
            //传入的是翻译
            Dictionary dictionaryByTranslation = dictionaryService.getDictionaryByWordTranLike(dictionary);
            if (dictionaryByTranslation == null) {
                System.err.println("根据“翻译”模糊查询失败,输入值为:[" + dictionary.getTranslation() + "]");
                //根据翻译模糊查询不到单词
                return JsonResult.error("找不到该单词");
            } else {
                System.err.println("根据“翻译”模糊查询成功,输入值为:[" + dictionary.getTranslation() + "],结果为:[" + dictionaryByTranslation + "]");
                //根据翻译模糊查询到单词
                return JsonResult.success(dictionaryByTranslation);
            }
        } else {
            //传入的是单词
            //先进行单词的精确查询
            System.err.println("传入的单词不为空，开始进行单词精确查询...");
            Dictionary dictionaryByWord = dictionaryService.getDictionaryByWord(dictionary);
            if (dictionaryByWord == null) {
                System.err.println("单词精确查询失败，开始进行单词的模糊查询...");
                //精确查询不到后进行模糊查询
                dictionary.setWord(dictionary.getWord().substring(0,(dictionary.getWord().length())/2 + 1));
                Dictionary dictionaryByWordLike = dictionaryService.getDictionaryByWordTranLike(dictionary);
                if (dictionaryByWordLike == null) {
                    System.err.println("根据“单词”模糊查询失败,输入值为:[" + dictionary.getWord() + "]");
                    //模糊查询不到结果
                    return JsonResult.error("找不到该单词");
                } else {
                    System.err.println("根据“单词”模糊查询成功,输入值为:[" + dictionary.getWord() + "],结果为:[" + dictionaryByWordLike + "]");
                    //模糊查询到结果
                    return JsonResult.success(dictionaryByWordLike);
                }
            } else {
                System.err.println("单词精确查询成功,输入值为:[" + dictionary.getWord() + "],结果为:[" + dictionaryByWord + "]");
                //精确查询直接查找到单词
                return JsonResult.success(dictionaryByWord);
            }
        }
    }

    /**
     * 用户添加收藏单词
     *
     * @param favouriteHistory 客户端传入参数
     * @return 返回Json
     */
    @RequestMapping("/doAddFavouriteWord")
    public @ResponseBody
    JsonResult<T> doAddFavouriteWord(@RequestBody FavouriteHistory favouriteHistory) {
        int flag = favouriteHistoryService.doAddFavouriteWord(favouriteHistory);
        if (flag > 0) {
            System.err.println("用户:[" + favouriteHistory.getUserId() + "]添加了收藏单词:[" + favouriteHistory.getDictionaryId() + "]");
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }

    /**
     * 用户添加收藏视听
     *
     * @param favouriteHistory 客户端传入参数
     * @return 返回Json
     */
    @RequestMapping("/doAddFavouriteResources")
    public @ResponseBody
    JsonResult<T> doAddFavouriteResources(@RequestBody FavouriteHistory favouriteHistory) {
        int flag = favouriteHistoryService.doAddFavouriteResources(favouriteHistory);
        if (flag > 0) {
            System.err.println("用户:[" + favouriteHistory.getUserId() + "]添加了收藏视听:[" + favouriteHistory.getResourceId() + "]");
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }

    /**
     * 用户删除收藏单词
     *
     * @param favouriteHistory 客户端传入参数
     * @return 返回Json
     */
    @RequestMapping("/doRemoveFavouriteWord")
    public @ResponseBody
    JsonResult<T> doRemoveFavouriteWord(@RequestBody FavouriteHistory favouriteHistory) {
        favouriteHistoryService.doRemoveFavouriteWord(favouriteHistory);
        System.err.println("用户:[" + favouriteHistory.getUserId() + "]删除了收藏单词:[" + favouriteHistory.getDictionaryId() + "]");
        return JsonResult.success();
    }

    /**
     * 用户删除收藏视听
     *
     * @param favouriteHistory 客户端传入参数
     * @return 返回Json
     */
    @RequestMapping("/doRemoveFavouriteResources")
    public @ResponseBody
    JsonResult<T> doRemoveFavouriteResources(@RequestBody FavouriteHistory favouriteHistory) {
        favouriteHistoryService.doRemoveFavouriteResources(favouriteHistory);
        System.err.println("用户:[" + favouriteHistory.getUserId() + "]删除了收藏视听:[" + favouriteHistory.getResourceId() + "]");
        return JsonResult.success();
    }

    /**
     * 用户添加历史单词
     *
     * @param favouriteHistory 客户端传入参数
     * @return 返回Json
     */
    @RequestMapping("/doAddHistoryWord")
    public @ResponseBody
    JsonResult<T> doAddHistoryWord(@RequestBody FavouriteHistory favouriteHistory) {
        int flag = favouriteHistoryService.doAddHistoryWord(favouriteHistory);
        if (flag > 0) {
            System.err.println("用户:[" + favouriteHistory.getUserId() + "]添加了历史单词:[" + favouriteHistory.getDictionaryId() + "]");
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }

    /**
     * 用户添加历史视听
     *
     * @param favouriteHistory 客户端传入参数
     * @return 返回Json
     */
    @RequestMapping("/doAddHistoryResources")
    public @ResponseBody
    JsonResult<T> doAddHistoryResources(@RequestBody FavouriteHistory favouriteHistory) {
        int flag = favouriteHistoryService.doAddHistoryResources(favouriteHistory);
        if (flag > 0) {
            System.err.println("用户:[" + favouriteHistory.getUserId() + "]添加了历史视听:[" + favouriteHistory.getResourceId() + "]");
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }

    /**
     * 用户删除历史单词
     *
     * @param favouriteHistory 客户端传入参数
     * @return 返回Json
     */
    @RequestMapping("/doRemoveHistoryWord")
    public @ResponseBody
    JsonResult<T> doRemoveHistoryWord(@RequestBody FavouriteHistory favouriteHistory) {
        favouriteHistoryService.doRemoveHistoryWord(favouriteHistory);
        System.err.println("用户:[" + favouriteHistory.getUserId() + "]删除了历史单词:[" + favouriteHistory.getDictionaryId() + "]");
        return JsonResult.success();
    }

    /**
     * 用户删除历史视听
     *
     * @param favouriteHistory 客户端传入参数
     * @return 返回Json
     */
    @RequestMapping("/doRemoveHistoryResources")
    public @ResponseBody
    JsonResult<T> doRemoveHistoryResources(@RequestBody FavouriteHistory favouriteHistory) {
        favouriteHistoryService.doRemoveHistoryResources(favouriteHistory);
        System.err.println("用户:[" + favouriteHistory.getUserId() + "]删除了历史视听:[" + favouriteHistory.getResourceId() + "]");
        return JsonResult.success();
    }

}
